﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using SBPweb.Communication.Shared.Library.Conversations;

using SBPweb.Communication.Client.Library.Xmpp.Conversations;

namespace SBPweb.Communication.Client.Presentation.Silverlight.Controls
{

    public partial class ConferenceView : UserControl
    {

        #region Private fields

        private Conference conference;
        //private Point lastCursorPosition;

        #endregion

        #region Constructor logic

        public ConferenceView()
        {
            InitializeComponent();
        }

        public ConferenceView(Conference conference)
            : this()
        {
            this.conference = conference;
            this.conference.MessageReceived += new EventHandler<MessageBaseEventArgs>(conference_MessageReceived);
            this.Tag = conference.Title;
            this.rtEditor.EnableSpellCheck = true;
            this.rtEditor.EnableURLRecognition = true;
            this.rtEditor.EnablePatternRecognition = true;
            this.rtEditor.MouseLeftButtonUp += new MouseButtonEventHandler(rtEditor_MouseLeftButtonUp);
            this.rtEditor.CursorMoved += new Liquid.RichTextBoxEventHandler(rtEditor_CursorMoved);
            this.rtEditor.CheckWord += new Liquid.RichTextBoxEventHandler(rtEditor_CheckWord);
        }

        #endregion

        #region Property accessors

        public ConversationBase Conversation
        {
            get
            {
                return conference;
            }
        }

        #endregion

        #region Methods

        private void SendOutgoingMessage(string message)
        {
            conference.SendMessage(message);
            //tbOutgoing.Text = string.Empty;
        }

        #region Event handlers

        private void conference_MessageReceived(object sender, MessageBaseEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate
            {
                if (!e.Message.IsMyOwn)
                {
                    object o = e.Message.Body;
                    if (o is string)
                    {
                        rtEditor.RichText = o.ToString();
                    }

                    //if (o is int)
                    //{
                    //    rtEditor.InsertText(((char)((int)o)).ToString());
                    //}

                    //if (o is Point)
                    //{
                    //    Point p = (Point)o;
                    //    if (p.X > 0)
                    //    {
                    //        rtEditor.CursorRight();
                    //    }
                    //    if (p.X < 0)
                    //    {
                    //        rtEditor.CursorLeft();
                    //    }
                    //    if (p.Y > 0)
                    //    {
                    //        rtEditor.CursorDown();
                    //    }
                    //    if (p.Y < 0)
                    //    {
                    //        rtEditor.CursorUp();
                    //    }
                    //}
                }
            });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //SendOutgoingMessage();
        }

        private void rtEditor_ContentChanged(object sender, Liquid.RichTextBoxEventArgs e)
        {
        }

        private void rtEditor_KeyUp(object sender, KeyEventArgs e)
        {
            //conference.SendMessage(new Message(e.Key.ToString()));
            //conference.SendMessage(new Commands.KeyUpMessage(e));
        }

        private void rtEditor_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //conference.SendMessage(new Commands.CursorMoveMessage(this.rtEditor.GetCursorCoordinates()));
        }

        private void rtEditor_CursorMoved(object sender, Liquid.RichTextBoxEventArgs e)
        {
            //Point p = rtEditor.GetCursorCoordinates();
            //Point delta = new Point(p.X - lastCursorPosition.X, p.Y - lastCursorPosition.Y);
            //if (delta.X != 0 || delta.Y != 0)
            //{
            //    conference.SendMessage(new Commands.CursorMoveMessage(delta));
            //}
            //lastCursorPosition = p;
        }

        private void rtEditor_CheckWord(object sender, Liquid.RichTextBoxEventArgs e)
        {
            //conference.SendMessage(e.Parameter.ToString());
            conference.SendMessage(rtEditor.RichText);
            e.Cancel = true;
        }

        #endregion

        #endregion

    }

}

